package com.jlz.actions.user;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Standard;
import org.springframework.beans.factory.annotation.Autowired;

import com.jlz.beans.user.LinkBean;
import com.jlz.beans.user.UserBean;
import com.jlz.managers.user.Links;
import com.jlz.managers.user.Users;
import com.julewa.Client;
import com.julewa.db.BeanValidator;

/*
 * 一般来说,IndexAction主要集中简单的跳转进入
 */

@Standard(rest="user.ID")
public class IndexAction { 
	private @Autowired Client client;
	private @Autowired BeanValidator validator;
	private @Autowired Links links;
	private @Autowired Users users;
	private @Autowired UserBean user;
	private String password;
	private String newpassword;
	private String repassword;	
	private LinkBean link;
	private List<LinkBean> linkList;
	//缺省进入client的home页面
	//如果没有登录,则进入登录页面
	public String execute(){
		return "home";
	}
	
	
	//用户个人信息单独放到目录里好了
	@Action("displayinfo")
	public String displayInfo(){
		user = (UserBean) client.getUser();
		return "edit";
	}	
	
	@Action("modifyinfo")
	public String modifyInfo(){
		users.update(user);
		client.setUser(user);
		return "home";
	}	
	
	@Action("cancel")
	public String cancel(){
		user = (UserBean) client.getUser();
		return "home";
	}
	
	@Action("displaypass")
	public String displayPass(){
		user = (UserBean) client.getUser();
		link = (LinkBean) client.getSession("currentlink");
		linkList = user.getLinks();
		return "editPwd";
	}
	
	@Action("modifypass")
	public String modifyPass(){
		user = (UserBean) client.getUser();
		link = (LinkBean) client.getSession("currentlink");
		
		linkList = user.getLinks();
		
		if(!link.getPassword().equals(password)){
			client.putError("password","输入密码错误！");
		}
		System.out.println(client.hasError());
		if(validator.checkField("newpassword", "新密码", newpassword, 6, 60)){
			if(!newpassword.equals(repassword)){
				client.setError("repassword", "两次输入的新密码不一致");
			}
		}
		
		if (client.hasError()) {
			client.putMessage("修改密码失败！");
			return "editPwd";
		}
		
		link.setPassword(newpassword);
		links.update(link);
		client.putMessage("修改密码成功！");
		client.setSession("currentlink",link);
		return "home";
	}	
	
	
	public UserBean getUser() {
		return user;
	}
	public void setUser(UserBean user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getNewpassword() {
		return newpassword;
	}
	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}
	public String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public LinkBean getLink() {
		return link;
	}

	public void setLink(LinkBean link) {
		this.link = link;
	}

	public List<LinkBean> getLinkList() {
		return linkList;
	}

	public void setLinkList(List<LinkBean> linkList) {
		this.linkList = linkList;
	}
	
}
